package com.javaee.controller.user;

import com.javaee.BeforeBaseController;
import com.javaee.entity.Board;

import com.javaee.service.admin.BoardService;
import com.javaee.service.user.UBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UBoardController extends BeforeBaseController {
    @Autowired
    private BoardService boardService;
    @Autowired
    private UBoardService uBoardService;
    @RequestMapping("/lastedBoardList")
    public String lastedBoardList(String keyword, Model model, Integer currentPage){
        if(keyword == null){
            keyword = "";
        }
        if (currentPage == null) {
            currentPage = 1;
        }
        int totalCount = boardService.selectCount();
        System.out.println("totalCount = " + totalCount);
        int pageSize = 3;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<Board> boards = boardService.searchBoard(keyword,(currentPage-1)*pageSize, pageSize);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("announcements", boards);
        return "showBoard";
    }
    @RequestMapping("/aBoardDetail")
    public String aBordDetail(Model model,int id){
        Board boardDetail =uBoardService.getNoticeById(id);
        model.addAttribute("boardDetail", boardDetail);
        return "aBoardDetails";
    }

}
